package com.nefu.softlab.xjwc.controller;

import com.nefu.softlab.xjwc.model.User;
import com.nefu.softlab.xjwc.service.UserService;
import com.nefu.softlab.xjwc.utils.JSONResult;
import com.nefu.softlab.xjwc.utils.JsonUtil;
import com.nefu.softlab.xjwc.utils.LoggerUtil;
import com.nefu.softlab.xjwc.utils.XJWCException;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * @author : Ar1es
 * @date : 2020/1/16
 * @since : Java 8
 */
@RestController
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*")
public class UserController {

    private final LoggerUtil logger;
    private final UserService userService;

    @Autowired
    public UserController(LoggerUtil logger, UserService userService) {
        this.logger = logger;
        this.userService = userService;
    }

    /**
     * 添加用户
     * @param user
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public JSONResult register(@RequestBody User user) throws XJWCException {
        if (ObjectUtils.isEmpty(user)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("POST register : " + JsonUtil.getJsonString(user));

        return userService.insertUser(user);
    }

    @RequiresRoles("admin")
    @PostMapping("/admin/add")
    public JSONResult addAdmin(@RequestBody User user) throws XJWCException {
        if (ObjectUtils.isEmpty(user)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("POST addAdmin , user : " + JsonUtil.getJsonString(user));
        return JSONResult.ok(userService.addAdmin(user));
    }


    /**
     * 专家修改密码
     * @param user
     * @return
     */
    @RequiresRoles("doctor")
    @RequestMapping(value = "/updatePassword", method = RequestMethod.PUT)
    public JSONResult updatePassword(@RequestBody User user) throws XJWCException {
        logger.info("PUT updatePassword : " + JsonUtil.getJsonString(user));
        int flag = userService.updateUserPassword(user);
        if (0 < flag) {
            return JSONResult.ok(flag);
        }
        return JSONResult.errorMessage("修改失败");
    }

    /**
     * 专家修改个人信息
     * @param user
     * @return
     */
    @RequiresRoles("doctor")
    @RequestMapping(value = "/updateInfo", method = RequestMethod.PUT)
    public JSONResult updateInfo(@RequestBody User user) throws XJWCException  {
        logger.info("PUT updateInfo : " + JsonUtil.getJsonString(user));
        int flag = userService.updateUserInfo(user);
        if (0 < flag) {
            return JSONResult.ok(flag);
        }
        return JSONResult.errorMessage("修改失败");
    }

    /**
     * 根据传入的role查询所有用户
     * @param user
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public JSONResult selectAllUser(@RequestBody(required = false) User user) throws XJWCException {
        logger.info("POST selectAllUser : " + JsonUtil.getJsonString(user));
        return JSONResult.ok(userService.selectAllUser(user));
    }

    /**
     * 根据ID删除用户
     * @param userId
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @DeleteMapping("/user/{userId}")
    public JSONResult deleteUserById(@PathVariable("userId") String userId) throws XJWCException {
        if (StringUtils.isEmpty(userId)) {
            return JSONResult.errorMessage("用户id不能为空!");
        }
        logger.info("DELETE deleteUserById : userId = " + userId);
        return JSONResult.ok(userService.deleteUserById(userId));
    }

    /**
     * 用户详细信息
     * @param user
     * @return
     * @throws XJWCException
     */
    @RequiresRoles("admin")
    @RequestMapping(value = "/userInfo", method = RequestMethod.POST)
    public JSONResult selectUserInfo(@RequestBody User user) throws XJWCException {
        if (ObjectUtils.isEmpty(user)) {
            return JSONResult.errorMessage("输入无效, 请重新输入!");
        }
        logger.info("POST selectUserInfo : " + JsonUtil.getJsonString(user));
        return JSONResult.ok(userService.selectUserInfo(user));
    }
}
